const { getBooksSql, addBooksSql, updateBooksSql, deleteBooksSql } = require('../model/books')
const { useBooksData, useLogin } = require('../utils/use')

/**
 * 获取所有书签
 * @path /api/books
 * @param {*} req { page, limit}
 * @param {*} res
 */
exports.getBooks = async (req, res) => {
  try {
    const { page = 1, limit = 15 } = req.query
    const { result, total } = await getBooksSql(
      Number(page),
      Number(limit),
      useLogin(req?.headers?.authorization)
    )

    // 如果页数大于总页数
    if (page > Math.ceil(total / limit)) {
      return res.jsonSuccess({
        result: [],
        total
      })
    }

    res.jsonSuccess({
      data: useBooksData(result),
      page: Number(page),
      limit: Number(limit),
      total
    })
  } catch (error) {
    console.log('获取书签失败', error.message)
    res.jsonFail(400, error.message || '获取书签失败')
  }
}

/***
 * 添加书签
 * @path /api/books/add
 * @param {*} req
 * @param {*} res
 */
exports.addBooks = async (req, res) => {
  try {
    const { book_url, book_name, book_desc = '', books_id, status = 1 } = req.body

    // 参数校验
    if (!book_url || !book_name || !book_desc || !books_id) {
      return res.jsonFail(400, '参数错误')
    }

    const book_id = useCreateID()

    // 添加书签
    const result = await addBooksSql(book_id, book_url, book_name, book_desc, books_id, status)

    // 判断是否添加成功
    if (!result) {
      return res.jsonFail(400, '添加失败')
    }

    res.jsonSuccess({
      id: book_id,
      name: book_name,
      url: book_url,
      description: book_desc,
      parent_id: books_id,
      status: Number(status) === 1 ? 'public' : 'private'
    })
  } catch (error) {
    res.jsonFail(400, error.message || '添加失败')
  }
}

/**
 * 修改书签
 * @path /api/books/update
 * @param {*} req
 * @param {*} res
 * @returns
 */
exports.updateBooks = async (req, res) => {
  try {
    const { book_id, book_url, book_name, book_desc, books_id, status = 1 } = req.body

    // 参数校验
    if (!book_id || !book_url || !book_name || !book_desc || !books_id) {
      return res.jsonFail(400, '参数错误')
    }

    // 修改书签
    const result = await updateBooksSql(
      book_id,
      book_url,
      book_name,
      book_desc,
      books_id,
      Number(status)
    )

    console.log('result', result)

    // 判断是否修改成功
    if (result.affectedRows === 0) {
      return res.jsonFail(400, '修改失败')
    }

    res.jsonSuccess({
      id: book_id,
      name: book_name,
      url: book_url,
      description: book_desc,
      parent_id: books_id,
      status: Number(status) === 1 ? 'public' : 'private'
    })
  } catch (error) {
    res.jsonFail(400, error.message || '修改失败')
  }
}

/**
 * 删除书签
 * @path /api/books/delete
 * @param {*} req
 * @param {*} res
 */
exports.deleteBooks = async (req, res) => {
  try {
    const { book_id } = req.body

    // 参数校验
    if (!book_id) {
      return res.jsonFail(400, '参数错误')
    }

    // 删除书签
    const result = await deleteBooksSql(book_id)

    // 判断是否删除成功
    if (!result) {
      return res.jsonFail(400, '删除失败')
    }

    res.jsonSuccess()
  } catch (error) {
    res.jsonFail(400, error.message || '删除失败')
  }
}
